package com.leetcode.根据数据结构分类.数组;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaomi
 * @date: 2021/3/21
 * @description:    73. 矩阵置零
 * https://leetcode-cn.com/problems/set-matrix-zeroes/
 */
public class B_73_矩阵置零 {

    /**
     *
     * @param matrix
     */
    public void setZeroes(int[][] matrix) {
        int m= matrix.length;
        int n =matrix[0].length;
        List<int[]> list = new ArrayList<int[]>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j]==0){
                    list.add(new int[]{i,j});
                }
            }
        }
        if (list.isEmpty()){
            return;
        }
        for (int i = list.size()-1; i >= 0; i--) {
            putZero(list.get(i),matrix,m,n);
        }
    }

    private void putZero(int[] arr, int[][] matrix, int m, int n) {
        for (int i = 0; i < m; i++) {
            //处理同一列
            matrix[i][arr[1]] =0;
        }
        for (int i = 0; i < n; i++) {
            //处理同一行
            matrix[arr[0]][i] =0;
        }
    }
}
